/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bef.dtgenerator.api.entityInterface;

import com.inspur.edp.bef.api.be.IBENodeEntity;
import com.inspur.edp.bef.api.be.IBusinessEntity;
import com.inspur.edp.bef.bizentity.GspBizEntityObject;
import com.inspur.edp.bef.bizentity.operation.BizAction;
import com.inspur.edp.bef.bizentity.operation.BizOperation;
import com.inspur.edp.bef.bizentity.operation.internalbeaction.IInternalBEAction;
import com.inspur.edp.bef.dtgenerator.api.ApiHelper;
import com.inspur.edp.bef.dtgenerator.api.BefApiGeneratorContext;
import com.inspur.edp.bef.dtgenerator.api.entityInterface.entityInfo.BeEntityInfoGenerator;
import com.inspur.edp.bef.dtgenerator.api.entityInterface.resourceInfo.BefEntityResourceInfoGenerator;
import com.inspur.edp.bef.dtgenerator.common.Utils;
import com.inspur.edp.caf.generator.BaseGenerator;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.cef.api.dataType.base.ICefDataType;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.cef.generator.apiGenerator.dataType.entityInterface.dataTypeInfo.DataTypeInfoGenerator;
import com.inspur.edp.cef.generator.apiGenerator.dataType.entityInterface.resourceInfo.EntityResourceInfoGenerator;
import com.inspur.edp.cef.generator.apiGenerator.entity.separateInterface.EntityInterfaceGenCtx;
import com.inspur.edp.cef.generator.apiGenerator.entity.separateInterface.EntityInterfaceGenerator;
import com.inspur.edp.das.commonmodel.IGspCommonObject;
import java.util.ArrayList;
import java.util.List;

public class BEEntityInterfaceGenerator extends EntityInterfaceGenerator {

  // region Static Members
  private static final String namePrefix = Utils.InterfacePrefix;
  private static final String nameSuffix = "BizEntity";
  private BefApiGeneratorContext apiGenContext;
  private GspBizEntityObject beObject;

  public BEEntityInterfaceGenerator(
      BefApiGeneratorContext apiGenContext, GspBizEntityObject beObject) {
    super(apiGenContext, beObject);
    this.apiGenContext = apiGenContext;
    this.beObject = beObject;
  }

  public static String getName(GspBizEntityObject node) {
    return namePrefix + node.getCode() + nameSuffix;
  }

  @Override
  protected EntityInterfaceGenerator createChildEntityGenerator(IGspCommonObject childObject) {
    return new BEEntityInterfaceGenerator(
        apiGenContext,
        (GspBizEntityObject) ((childObject instanceof GspBizEntityObject) ? childObject : null));
  }

  @Override
  protected String getName() {
    return getName(beObject);
  }

  @Override
  protected java.util.ArrayList<com.inspur.edp.caf.generator.baseInfo.TypeInfo>
  addParentInterfaces() {
    return null;
    // if (!apiGenContext.getBeGenCtx().BE.HasParentBe())
    //    return null;
    // var parentInterfaceAssemblyName = apiGenContext.ParentAssemblyName;
    // var parentInterfaceClassName = string.Format("%1$s.{1}",
    //    apiGenContext.ParentNamespace,
    //    GetName(apiGenContext.getBeGenCtx().ParentBE.FindObjectByCode(beObject.getCode()) as
    // GspBizEntityObject));
    // return new List<Type>(1)
    // {
    //    ReflectionUtil.GetTypeFromPath(apiGenContext.getBeGenCtx().ParentGeneratePath,
    // parentInterfaceAssemblyName, parentInterfaceClassName)
    // };
  }

//
//  @Override
//  protected java.util.ArrayList<com.inspur.edp.caf.generator.baseInfo.TypeInfo> addParentInterfaces() {
//    List<TypeInfo> rez = new ArrayList<>();
//    String code = apiGenContext.getBeGenCtx().getCommonDataType().getCode();
//    MediateType generated = (MediateType) this.getContext().getApiGenContext().getEntityInterfaceTypes().get(code);
//    if (generated != null) {
//      rez.add(this.getDefaultInterfaceType());
//    }
//    if (beObject.getIsRef()) {
//      if (apiGenContext.getBeGenCtx().getIsParentBEInSameProj()) {
//        return null;
//      }
//      TypeInfo parentClassName = getParentClassName();
//      return new java.util.ArrayList<TypeInfo>(
//          java.util.Arrays.asList(parentClassName));
//    } else {
//      if (beObject.getIsRootNode()) {
//        rez.add(com.inspur.edp.cef.generator.overall.Utils.createTypeInfo(IBusinessEntity.class));
//      } else {
//        rez.add(com.inspur.edp.cef.generator.overall.Utils.createTypeInfo(IBENodeEntity.class));
//      }
//    }
//    return rez.size() == 0 ? null : (ArrayList<TypeInfo>) rez;
//    // if (!apiGenContext.getBeGenCtx().BE.HasParentBe())
//    //    return null;
//    // var parentInterfaceAssemblyName = apiGenContext.ParentAssemblyName;
//    // var parentInterfaceClassName = string.Format("%1$s.{1}",
//    //    apiGenContext.ParentNamespace,
//    //    GetName(apiGenContext.getBeGenCtx().ParentBE.FindObjectByCode(beObject.getCode()) as
//    // GspBizEntityObject));
//    // return new List<Type>(1)
//    // {
//    //    ReflectionUtil.GetTypeFromPath(apiGenContext.getBeGenCtx().ParentGeneratePath,
//    // parentInterfaceAssemblyName, parentInterfaceClassName)
//    // };
//  }

  @Override
  protected java.util.ArrayList<BaseGenerator> createEntityExtendGenerators() {
    java.util.ArrayList<BaseGenerator> list = super.createEntityExtendGenerators();
    if (list == null) {
      list = new java.util.ArrayList<BaseGenerator>();
    }
    EntityMethodBuildUtil.addBEActions(apiGenContext, beObject, list);
    return list;
  }

  @Override
  protected DataTypeInfoGenerator createDataTypeInfoGenerator() {
//    return new BeEntityInfoGenerator((EntityInterfaceGenCtx) getContext());
  return null;
  }

  @Override
  protected EntityResourceInfoGenerator createResourceInfoGenerator() {
    return new BefEntityResourceInfoGenerator(getContext(), beObject);
  }

//  private TypeInfo getParentClassName() {
//    MediateType mtype =
//        ApiHelper.getBeEntityAllInterfaceClassName(
//            apiGenContext.getBeGenCtx().getParentBE().findObjectByCode(beObject.getCode()));
//    TypeInfo result = new TypeInfo(mtype.getClassName());
//    result.setTypePackageName(mtype.getNamespace());
//    return result;
//  }

  protected TypeInfo getDefaultInterfaceType() {
    return new TypeInfo(ICefDataType.class);
  }
  // endregion
}
